package com.kun.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.kun.music.domain.Song;
import com.kun.music.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author fangkun
 * @create 2022-12-21-11:45
 */
@RestController
public class SongController {

    @Autowired
    private SongService songService;

    @Configuration
    public class MyPicConfig implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/song/**").addResourceLocations("file:E:\\workSpace\\music-website-master\\music-server\\song\\");
        }
    }

    @GetMapping("/song")
    public Object allSong(){
        return songService.allSong();
    }

    @GetMapping("/song/detail")
    public Object getSongOfId(@RequestParam("id") int id){
        return songService.getSongOfId(id);
    }

    @GetMapping("/song/singer/detail")
    public Object getSongOfSingerId(@RequestParam("singerId") int id){
        return songService.getSongOfSingerId(id);
    }

    @PostMapping("song/update")
    public Object updateSongMsg(HttpServletRequest req){
        JSONObject jsonObject = new JSONObject();

        String id = req.getParameter("id").trim();
        String singer_id = req.getParameter("singerId").trim();
        String name = req.getParameter("name").trim();
        String introduction = req.getParameter("introduction").trim();
        String lyric = req.getParameter("lyric").trim();

        Song song = new Song();
        song.setId(Integer.parseInt(id));
        song.setSingerId(Integer.parseInt(singer_id));
        song.setName(name);
        song.setIntroduction(introduction);
        song.setUpdateTime(new Date());
        song.setLyric(lyric);

        boolean res = songService.updateSongMsg(song);

        if (res){
            jsonObject.put("code", 1);
            jsonObject.put("msg", "修改成功");
            return jsonObject;
        }else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "修改失败");
            return jsonObject;
        }
    }

    @GetMapping("/song/delete")
    public Object deleteSong(@RequestParam("id") int id){
        JSONObject jsonObject = new JSONObject();

        boolean res = songService.deleteSong(id);

        if (res){
            jsonObject.put("code", 1);
            jsonObject.put("msg", "删除成功");
            return jsonObject;
        }else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "删除失败");
            return jsonObject;
        }
    }

    @GetMapping("/song/singerName/detail")
    public Object getSongByName(@RequestParam String name){
        List<Song> list = songService.getSongByName('%'+ name + '%');
        return list;
    }

}
